<template>
  <div>
    <div class="login_container">
      <!-- 登录的表单 -->
      <div class="login_box">
        <!-- 登录的头像 -->
        <div class="avatar_box">
          <!-- 头像 -->
          <img src="../../assets/imgs/avater.png" alt="" />
        </div>

        <!-- 登录的表单 -->
        <el-form ref="ruleForm" :rules="rules" :model="form"  label-width="0px">
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="请输入用户名称"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              placeholder="请输入用户密码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div class="verifyCode_box">
              <el-input
                v-model="form.verifyCode"
                placeholder="请输入手机验证码"
                prefix-icon="el-icon-mobile"
                class="verifyCode"
              ></el-input>
              <img
                src="http://localhost:8888/captcha"
                alt=""
                class="verifyCode_img"
                width="130px" height="48px"
              />
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button></el-button>
            <el-button>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import { USERLOGIN, USERLOGOUT } from "../../store/types";
import { login } from "../../api/user";
export default {
  data() {
    return {
      form: {
        username: "xiaoge",
        password: "123456",
        verifyCode: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapMutations([USERLOGIN, USERLOGOUT]),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let result = await login(
            this.form.username,
            this.form.password,
            this.form.verifyCode
          );
          const token = result.data.token;
          if (token != "") {
            // 保存token 用于权限验证
            this[USERLOGIN](token);
            this.$router.push("/home");
          } else {
            this.$message({
              message: "你的账号密码错误",
              type: "error",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  height: 100vh;
  background-color: aquamarine;
  display: flex;
  // 主轴居中
  align-items: center;
  // 侧轴居中
  justify-content: center;

  .login_box {
    width: 450px;
    height: 380px;
    background-color: #ffffff;
    // 父相
    position: relative;
    padding: 20px;

    .avatar_box {
      width: 130px;
      height: 130px;
      background-color: #fff;
      border-radius: 50%;
      padding: 10px;
      // 子绝
      position: absolute;
      top: -75px;
      right: 170px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }

    .el-form {
      margin-top: 75px;

      .verifyCode_box {
        display: flex;
        align-items: center;

        .verifyCode {
          flex: 2;
        }

        .verifyCode_img {
          flex: 1;
        }
      }
    }
  }
}
</style>